# from apscheduler.schedulers.blocking import BlockingScheduler
#
# from database.mapper import stock_info, stock_day_k, stock_day_macd, stock_monitor
# from utils import date_util, k_util_v1, macd_util_v1
#
#
# # 数据刷新定时任务
# def fresh_data():
#     # 启动先刷新一次任务
#     fresh_task_interval()
#     # 添加任务,时间间隔30S
#     scheduler = BlockingScheduler()
#     # 从开始时间到结束时间，每隔一小时运行一次
#     scheduler.add_job(fresh_task_interval, 'interval', seconds=1800, id='monitor_task')
#     # 开市插入新数据
#     scheduler.add_job(add_new_task, 'cron', day_of_week='0-4', hour=9, minute=31)
#     # 闭市再次刷新数据
#     scheduler.add_job(fresh_task_close, 'cron', day_of_week='0-4', hour=21, minute=20)
#     scheduler.start()
#
#
# # 开市插入新数据
# # 已完成
# def add_new_task():
#     if not date_util.valid_day(date_util.get_date_str()):
#         return
#     s_list = stock_info.selectall()
#     # 增加日K
#     add_day_k(s_list)
#     # 增加日macd
#     add_day_macd(s_list)
#
#
# # 已完成
# def add_day_k(s_list):
#     for s in s_list:
#         code = s['code']
#         day_k = stock_day_k.select(code)
#         if len(day_k) == 0:
#             k_util_v1.init_day_k(code)
#         else:
#             o_rq = day_k['rq']
#             d_rq = date_util.str_to_date(date_util.get_date_str(), '%Y-%m-%d')
#             diff_d = date_util.diff_days(o_rq, d_rq)
#             if diff_d <= 0:
#                 print('日期一致end，code' + code)
#                 continue
#             k_lines = k_util_v1.get_day_k_size(code, diff_d + 1)
#             k_len = len(k_lines)
#             if k_len == 0:
#                 continue
#             for i in range(k_len):
#                 if date_util.str_to_date(k_lines[i]['rq'], '%Y-%m-%d').date() == o_rq:
#                     # 更新当天数据
#                     stock_day_k.update(k_lines[i])
#                     # 插入剩余数据
#                     if i >= 1:
#                         stock_day_k.insert_multiple(k_lines[0: i])
#                     break
#
#
# def add_day_macd(s_list):
#     for s in s_list:
#         code = s['code']
#         day_macd = stock_day_macd.select(code)
#         if len(day_macd) == 0:
#             macd_util_v1.init_day_macd(code)
#         else:
#             o_rq = day_macd['rq']
#             d_rq = date_util.str_to_date(date_util.get_date_str(), '%Y-%m-%d')
#             diff_d = date_util.diff_days(o_rq, d_rq)
#             if diff_d <= 0:
#                 print('日期一致end，code' + code)
#                 continue
#             day_k_list = stock_day_k.select_interval(code, o_rq, d_rq)
#             # 日期从小到大
#             day_k_list.reverse()
#             macd_up_list = []
#             for day_k in day_k_list:
#                 k_rq = day_k['rq']
#                 if k_rq == o_rq:
#                     continue
#                 macd_up_list.append(macd_util_v1.cal_macd_next_v1(day_macd, k_rq, day_k['sp']))
#             stock_day_macd.insert_multiple(macd_up_list)
#
#
# # 闭市刷新数据
# def fresh_task_close():
#     print("闭市开始刷新当日数据")
#     if not date_util.valid_day(date_util.get_date_str()):
#         return
#     s_list = stock_info.selectall()
#     fresh_task(s_list)
#
#
# # 刷新数据
# def fresh_task_interval():
#     if not check_time():
#         return
#     s_list = stock_monitor.select_distinct()
#     fresh_task(s_list)
#
#
# # 刷新数据
# def fresh_task(s_list):
#     for stock in s_list:
#         code = stock['code']
#         # 一、刷新日K
#         k_line = k_util_v1.get_day_k_size(code, 1)
#         if not k_line or len(k_line) == 0:
#             print('code' + code + '停牌？')
#             continue
#         for k in k_line:
#             count = stock_day_k.update(k)
#             if count == 0:
#                 add_day_k([{'code': code}])
#         # 二、刷新日macd
#         for k in k_line:
#             rq = k['rq']
#             macd_list = stock_day_macd.select_newest_size(code, 2)
#             if len(macd_list) == 0 or date_util.date_to_str(macd_list[0]['rq'], '%Y-%m-%d') != rq:
#                 add_day_macd([{'code': code}])
#                 break
#             next_macd = macd_util_v1.cal_macd_next_v1(macd_list[1], rq, float(k['sp']))
#             count = stock_day_macd.update(next_macd)
#             if count == 0:
#                 add_day_macd([{'code': code}])
#
#
# # 检查有效时间
# def check_time():
#     if not date_util.valid_day(date_util.get_date_str()):
#         return 0
#     # 触发任务一（up_down_monitor）
#     date_time = date_util.get_date_time()
#     hour = date_time.hour
#     minute = date_time.minute
#     if 9 <= hour < 15 and not (hour == 9 and minute < 30) \
#             and not (hour == 11 and minute > 30) and not (hour == 12):
#         return 1
#     return 0
#
#
# # 校验数据
# def check_data():
#     s_list = stock_info.selectall()
#     count = 0
#     for s in s_list:
#         if count >= 5000:
#             return
#         code = s['code']
#         o_count = len(k_util_v1.get_day_k_size(code, 12000))
#         k_count = stock_day_k.select_count(code)
#         macd_count = stock_day_macd.select_count(code)
#         if o_count != k_count:
#             print(code + '线上和线下数据不一致')
#             stock_day_k.delete_interval(code, '1990-01-01')
#             stock_day_macd.delete_all(code)
#             count += 1
#             continue
#         if k_count != macd_count:
#             print(code + 'k+macd数据不一致')
#             stock_day_macd.delete_all(code)
#             count += 1
#
#
# if __name__ == '__main__':
#     # 已完成
#     s_list = [{'code': '688687'},
#               {'code': '688059'}
#               ]
#     add_day_k(s_list)
